﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class AlmacenSedeController : Controller
    { 
        private List<AlmacenSede> CrearAlmacenSede()
        {
            Sede sanborja = new Sede() { Codigo = 1, Nombre = "San Borja" };
            Sede sanisidro = new Sede() { Codigo = 2, Nombre = "San Isidro" };
            Almacen almacen1 = new Almacen() { Codigo = 1, Nombre = "almacen1" };
            Almacen almacen2 = new Almacen() { Codigo = 2, Nombre = "almacen2" };
            List<AlmacenSede> almacensedes = new List<AlmacenSede>();
            almacensedes.Add(new AlmacenSede() { Codigo = 1, Almacen = almacen1, Sede = sanborja, Producto = "Lapicero", StockMin = 4, StockMax = 10, Stock = 5 });
            almacensedes.Add(new AlmacenSede() { Codigo = 2, Almacen = almacen2, Sede = sanisidro, Producto = "Lapicero", StockMin = 8, StockMax = 15, Stock = 10 });
            return almacensedes;
        }

        private AlmacenSede ObtenerAlmacenSede(int Codigo)
        {
            //List<AlmacenSede> almacensedes = (List<AlmacenSede>)Session["almacensede"];
            List<AlmacenSede> almacensedess = (List<AlmacenSede>)Session["almacensedes"];
            AlmacenSede model = almacensedess.Single(delegate(AlmacenSede almacensede)
            {
                    if (almacensede.Codigo == Codigo) return true;
                    else return false;     
            });
        return model;
        }
     

        //
        // GET: /AlmacenSede/ 
        public ActionResult Index()
        {
            if (Session["almacensedes"] == null)
                Session["almacensedes"] = CrearAlmacenSede();
            List<AlmacenSede> model = (List<AlmacenSede>)Session["almacensedes"];
            return View(model);

 
        }

        //
        // GET: /AlmacenSede/Details/5

        public ActionResult Details(int id)
        {
            AlmacenSede model = ObtenerAlmacenSede(id);
            return View(model);
        }

        //
        // GET: /AlmacenSede/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /AlmacenSede/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                List<AlmacenSede> almacensedes = (List<AlmacenSede>)Session["almacensedes"];
                almacensedes.Add(new AlmacenSede()
                    {
                        Codigo = int.Parse(collection["Codigo"]),
                        Almacen = new Almacen(){
                            Codigo = int.Parse(collection["Almacen.Codigo"]),
                            Nombre = collection["Almacen.Nombre"]
                        },
                        Sede = new Sede(){
                            Codigo = int.Parse(collection["Sede.Codigo"]),
                            Nombre = collection["Sede.Nombre"]
                        },
                        Producto = collection["Producto"],
                        Stock = int.Parse(collection["Stock"]),
                        StockMin = int.Parse(collection["StockMin"]),
                        StockMax = int.Parse(collection["StockMax"]) 
                    });
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /AlmacenSede/Edit/5
 
        public ActionResult Edit(int id)
        {
            AlmacenSede model = ObtenerAlmacenSede(id);

            return View(model);
        }

        //
        // POST: /AlmacenSede/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                AlmacenSede model = ObtenerAlmacenSede(id);
                model.Stock = int.Parse(collection["Stock"]);
                model.StockMax = int.Parse(collection["StockMax"]);
                model.StockMin = int.Parse(collection["StockMin"]);
                model.Producto = collection["Producto"];
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AlmacenSede/Delete/5
 
        public ActionResult Delete(int id)
        {

            AlmacenSede model = ObtenerAlmacenSede(id);

            return View(model); 
        }

        //
        // POST: /AlmacenSede/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                List<AlmacenSede> almacensedes = (List<AlmacenSede>)Session["almacensedes"];
                almacensedes.Remove(ObtenerAlmacenSede(id));

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
         
    }
}
